<template>
  <div class="sidebar_item">
    <!-- 如果没有子菜单或只有一个二级的子菜单则直接渲染 -->
    <template
      v-if="
        hasOneShowingChild(item.children, item) &&
        (!onlyOneChild.children || onlyOneChild.noShowingChildren)
      "
    >
      <router-link
        v-if="onlyOneChild.meta"
        :to="resolvePath(onlyOneChild.path)"
      >
        <el-menu-item :index="resolvePath(onlyOneChild.path)" class="title">
          <!-- <span slot="title">{{ onlyOneChild.meta.title }}</span> -->
          <i v-if="item.meta && item.meta.icon" :class="item.meta.icon"></i>
          <span slot="title">{{ $t(`menu.${String(item.meta.title)}`) }}</span>
        </el-menu-item>
      </router-link>
    </template>

    <!-- 有子菜单 -->
    <el-submenu v-else :index="resolvePath(item.path)" popper-append-to-body>
      <template slot="title">
        <i v-if="item.meta && item.meta.icon" :class="item.meta.icon"></i
        ><!-- 加载图标 （拼接版）
        <i v-if="item.meta && item.meta.icon" :class="['el-icon-' + item.meta.icon]"></i>
        <span slot="title" class="title">{{ item.meta.title }}</span>-->
        <span slot="title" class="title">{{
          $t(`menu.${String(item.meta.title)}`)
        }}</span>
      </template>
      <SidebarItem
        v-for="(child, i) in item.children"
        :key="i"
        :item="child"
        :is-nested="true"
        :basePath="resolvePath(child.path)"
      ></SidebarItem>
    </el-submenu>
  </div>
</template>

<script>
import path from 'path'

export default {
  name: 'SidebarItem',
  data () {
    return {
      onlyOneChild: {
        children: []
      }
    }
  },
  props: {
    item: {
      type: Object,
      required: true
    },
    basePath: {
      type: String,
      default: ''
    }
  },

  methods: {
    // 判断是否只有一个子菜单
    hasOneShowingChild (children, parent) {
      // console.log('parent::: ',children , parent);
      if (!children) {
        children = []
      }

      // 过滤掉隐藏的菜单
      const showingChildren = children.filter((item) => {
        // 是否隐藏菜单
        if (item.hidden) {
          return false
        } else {
          this.onlyOneChild = item
          return true
        }
      })

      // 当只有一个子路由时，默认显示子路由器
      if (showingChildren.length === 1) {
        return true
      }

      // 如果没有子路由，则显示父级路由
      if (showingChildren.length === 0) {
        this.onlyOneChild = { ...parent, path: '', noShowingChildren: true }
        return true
      }
      return false
    },
    // 判断是否是外链
    isExternal (path) {
      return /^(https?:|mailto:|tel:)/.test(path)
    },
    // 路径拼接
    resolvePath (routePath) {
      if (this.isExternal(routePath)) {
        return routePath
      }
      if (this.isExternal(this.basePath)) {
        return this.basePath
      }
      return path.resolve(this.basePath, routePath).replace(/\/+/g, '/')
    }
  },
}
</script>

<style lang="scss" scoped>
.sidebar_item {
  cursor: pointer;
  > a {
    height: 60px;
    line-height: 60px;
    text-decoration: none;
  }
}
::v-deep .el-submenu__title {
  height: 60px;
  //line-height: 60px;
  text-decoration: none;
}

::v-deep .el-submenu__title:hover,
a :hover {
  background-color: #cecec8 !important;
  text-decoration: none;
}

::v-deep .active {
  background-color: #2f302f !important;
  text-decoration: none;
}

.is-active {
  background-color: #1b1d1c !important;
  text-decoration: none;
}

.title {
  font-size: 16px;
  text-decoration: none;
}
</style>
